package com.transformer.shobal.activitys.paint;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View;
import com.transformer.shobal.R;

/**
 * Created by tiny on 2015/1/11.
 */
public class PaintDemoView extends View{
    private int[] colors = new int[4];

    public PaintDemoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public PaintDemoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        colors[0] = getContext().getResources().getColor(R.color.top);
        colors[1] = Color.GREEN;
        colors[2] = getContext().getResources().getColor(R.color.middle2);
        colors[3] = Color.BLUE;
    }

    public PaintDemoView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 创建LinearGradient并设置渐变颜色数组
        // 第一个,第二个参数表示渐变起点 可以设置起点终点在对角等任意位置
        // 第三个,第四个参数表示渐变终点
        // 第五个参数表示渐变颜色
        // 第六个参数可以为空,表示坐标,值为0-1 new float[] {0.25f, 0.5f, 0.75f, 1 }
        // 如果这是空的，颜色均匀分布，沿梯度线。
        // 第七个表示平铺方式
        // CLAMP重复最后一个颜色至最后
        // MIRROR重复着色的图像水平或垂直方向已镜像方式填充会有翻转效果
        // REPEAT重复着色的图像水平或垂直方向
        Paint paint = new Paint();
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
        paint.setShader(new LinearGradient(0f, 0f, 100f, 100f, colors, null, Shader.TileMode.CLAMP));
        paint.setShadowLayer(2, 5, 5, getResources().getColor(R.color.live_nickname_shadow));
        canvas.drawRect(0,0,200,200,paint);
        super.onDraw(canvas);

        Paint paint1 = new Paint();
        float[] a1 = new float[]{0.2f,0.2f,0.5f,0.1f};
        paint1.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
        paint1.setShader(new RadialGradient(320f, 100f, 80f, colors,null , Shader.TileMode.CLAMP));
        paint1.setShadowLayer(2, 5, 5, getResources().getColor(R.color.live_nickname_shadow));
        canvas.drawRect(220,0,420,200,paint1);
        super.onDraw(canvas);

        Paint paint2 = new Paint();
        paint2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
        paint2.setShader(new SweepGradient(540f, 100f, colors,null));
        paint2.setShadowLayer(2, 5, 5, getResources().getColor(R.color.live_nickname_shadow));
        canvas.drawRect(440,0,640,200,paint2);
        super.onDraw(canvas);

        Paint paint3 = new Paint();
        paint3.setShader(new ComposeShader(
                new LinearGradient(0f, 240f, 0f, 440f, colors, null, Shader.TileMode.MIRROR),
                new RadialGradient(100f, 340f, 20f, colors, null, Shader.TileMode.MIRROR),
                PorterDuff.Mode.MULTIPLY));
        canvas.drawRect(0,240,200,440,paint3);
        super.onDraw(canvas);

        //ColorFilter
        Paint paint4 = new Paint();
        //paint4.setColor(Color.BLUE);
        paint4.setShader(new RadialGradient(100f, 560f, 80f, colors,null , Shader.TileMode.CLAMP));
        //paint4.setColorFilter(new PorterDuffColorFilter(Color.GREEN,PorterDuff.Mode.MULTIPLY));
        //paint4.setColorFilter(new LightingColorFilter(20,10));
        paint4.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(new float[]
                {1,0,0,0,100,
                0,1,0,0,100,
                0,0,1,0,100,
                0,0,0,1,0})));
        canvas.drawRect(0,460,200,660,paint4);
        super.onDraw(canvas);
    }
}
